import { GobangGameState, PieceType } from "./gobang/GobangGameState";
import { GobangMove } from "./gobang/GobangMove";
import { GobangPoint } from "./gobang/GobangPoint";
import { MCTS } from "./mcts_core/MCTS";
import type { MCTSGameMove } from "./mcts_core/MCTSGameMove";
import { GameResult } from "./mcts_core/MCTSGameState";
let N_SIMULATION_TIMES = 10000
let gameState = new GobangGameState(15)
const readline = require('readline');
function movePoint(){
    const rl = readline.createInterface({
        input: process.stdin,
        output: process.stdout
    });
    gameState.printConsole()
    rl.question('请您下子(o)[x y]：', (data: String) => {
        let place = data.split(' ')
        rl.close();
        let coord: number[] = []
        coord.push(parseInt(place[0]))
        coord.push(parseInt(place[1]))
        let point = new GobangPoint(coord, PieceType.Player)
        let move = new GobangMove(point)
        gameState.placePiece(move)
        let result = gameState.judgeLastMove();
        if(result!=GameResult.NoOutcome){
            if(result==GameResult.PlayerWon){
                console.log('您赢了');
                return
            }else if(result==GameResult.ComputerWon){
                console.log('很遗憾，您输了')
                return
            }else if(result==GameResult.Draw){
                console.log('平局')
                return
            }
        }
        //电脑落子
        let mcts = new MCTS(gameState, N_SIMULATION_TIMES);
        let MCTSMove: MCTSGameMove = mcts.decideMove()!;
        let nextMove: GobangMove = MCTSMove
        console.log(nextMove.point.coord)
        gameState.placePiece(nextMove)
        result = gameState.judgeLastMove();
        if(result!=GameResult.NoOutcome){
            if(result==GameResult.PlayerWon){
                console.log('您赢了');
            }else if(result==GameResult.ComputerWon){
                console.log('很遗憾，您输了')
            }else if(result==GameResult.Draw){
                console.log('平局')
            }
        }else{
            movePoint()
        }
        
    });

}
movePoint()